﻿using Application.PublicFileService;
using Domain.Entity.bs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class PublicFileController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;
        private readonly IPublicFileService publicFileService;

        public PublicFileController(IWebHostEnvironment webHostEnvironment,IPublicFileService publicFileService)
        {
            this.webHostEnvironment = webHostEnvironment;
            this.publicFileService = publicFileService;
        }

        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Uploadfile(IFormFileCollection files)
        {
            if (files == null || files.Count == 0)
            {
                return BadRequest("没有上传文件.");
            }

            var uploadedFiles = new List<object>();

            foreach (var file in files)
            {
                if (file.Length == 0) continue;

                // 生成文件名
                string name = DateTime.Now.ToString("yyyyMMddHHmmssfff"); // 添加毫秒和随机数以区分文件
                                                                          // 获取后缀名
                string ext = Path.GetExtension(file.FileName);
                // 设置上传路径
                string relativePath = $"Uploader/{DateTime.Now:yyyy-MM-dd}-{name}{ext}";
                // 生成文件的绝对路径
                var absolutePath = Path.Combine(webHostEnvironment.WebRootPath, relativePath);

                // 创建上传文件对应的文件夹
                (new FileInfo(absolutePath)).Directory.Create();

                using (var stream = new FileStream(absolutePath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }

                // 返回完整的可访问 URL
                string fullUrl = $"{Request.Scheme}://{Request.Host}/{relativePath.Replace("\\", "/")}";
                uploadedFiles.Add(new { success = true, path = fullUrl, originalName = file.FileName });
            }

            return Ok(uploadedFiles);
        }


        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="type">业务</param>
        /// <param name="code">业务编号</param>
        /// <param name="file">附件</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddFile(string? type, string? code, string? file, string? title)
        {
            return await publicFileService.AddFile(type, code, file, title);
        }



        /// <summary>
        /// 根据业务类型和编号查询对应的附件
        /// </summary>
        /// <param name="type">业务类型</param>
        /// <param name="code">对应编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PublicFile>> GetPublicFileBy(string? type, string? code)
        {
            return await publicFileService.GetPublicFileBy(type, code);
        }


        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="type">业务类型</param>
        /// <param name="code">对应编号</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteFile(string? type, string? code)
        {
            return await publicFileService.DeleteFile(type, code);
        }
    }
}
